import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CorePayChannelService extends BaseService<any> {
  private readonly logger = new Logger(CorePayChannelService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * findRecord (原方法名: find)
   * 对应 PHP: CorePayChannelService_core::find()
   * 逻辑类型: undefined - undefined
   * 注意: 为避免与BaseService方法冲突，已重命名
   */
  async findRecord(site_id: any[], where: any[]) {
    // 基于PHP真实逻辑: find
    // PHP原文: $where[ 'site_id' ] = $site_id;         return $this->model->where($where)->findOrEmpty();     }      /**      * 通过渠道获取支持的支付方式(专属用于支付业务)      * @param...
where[ 'site_id' ] = site_id;
        return this.model.where(where).findOrEmpty(];
    }

    /**
     * 通过渠道获取支持的支付方式(专属用于支付业务)
     * @param int site_id
     * @param string channel
     * @param string trade_type
     * @return array|array[]
     * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getAllowPayTypeByChannel(int site_id, string channel, string trade_type = '')
    {
        const channel_pay_list = this.model.where([ [ 'site_id', '=', site_id ], [ 'channel', '=', channel ], [ 'status', '=', 1 ) )).field('type,config').order('sort asc').select().toArray(];

        if (!!channel_pay_list)) {
            const temp_channel_pay_list = array_column(channel_pay_list, 'type'];
            const pay_type_arr = PayDict.getPayType(temp_channel_pay_list);
            foreach (temp_channel_pay_list as key) {
                if (typeof pay_type_arr[key))) {
                    pay_type_list[key] = pay_type_arr[key;
  }
  }

  /**
   * getAllowPayTypeByChannel
   * 对应 PHP: CorePayChannelService_core::getAllowPayTypeByChannel()
   * 逻辑类型: undefined - undefined
   */
  async getAllowPayTypeByChannel(site_id: number, channel: number, trade_type: number) {
    // 基于PHP真实逻辑: getAllowPayTypeByChannel
    // PHP原文: $channel_pay_list = $this->model->where([ [ 'site_id', '=', $site_id ], [ 'channel', '=', $channel ], [ 'status', '=', 1 ] ])->field('type,config')->o...
const channel_pay_list = this.model.where([ [ 'site_id', '=', site_id ], [ 'channel', '=', channel ], [ 'status', '=', 1 ) )).field('type,config').order('sort asc').select().toArray(];

        if (!!channel_pay_list)) {
            const temp_channel_pay_list = array_column(channel_pay_list, 'type'];
            const pay_type_arr = PayDict.getPayType(temp_channel_pay_list);
            foreach (temp_channel_pay_list as key) {
                if (typeof pay_type_arr[key))) {
                    pay_type_list[key] = pay_type_arr[key;
  }
  }

  /**
   * getConfigByChannelAndType
   * 对应 PHP: CorePayChannelService_core::getConfigByChannelAndType()
   * 逻辑类型: undefined - undefined
   */
  async getConfigByChannelAndType(site_id: number, channel: number, type: number) {
    // 基于PHP真实逻辑: getConfigByChannelAndType
    // PHP原文: $pay_channel = $this->model->where([ [ 'site_id', '=', $site_id ], [ 'channel', '=', $channel ], [ 'type', '=', $type ] ])->field('config')->findOrEmp...
const pay_channel = this.model.where([ [ 'site_id', '=', site_id ], [ 'channel', '=', channel ], [ 'type', '=', type ) )).field('config').findOrEmpty(];
        if (!pay_channel.isEmpty()) {
            if (type == PayDict.WECHATPAY) {
                pay_channel.config = Object.assign(pay_channel.config, this.getWechatPayFullConfig(site_id)];
            }
            return pay_channel.config;
  }

  /**
   * getWechatPayFullConfig
   * 对应 PHP: CorePayChannelService_core::getWechatPayFullConfig()
   * 逻辑类型: undefined - undefined
   */
  async getWechatPayFullConfig(site_id: number) {
    // 基于PHP真实逻辑: getWechatPayFullConfig
    // PHP原文: //TODO 先判断是否是开放平台授权,然后再决定使用什么appid         //查询公众号配置         $core_wechat_config_service = new CoreWechatConfigService();         $mp_app_id = $core_w...
//TODO 先判断是否是开放平台授权,然后再决定使用什么appid
        //查询公众号配置
        const core_wechat_config_service = this.coreWechatConfigService;
        const mp_app_id = core_wechat_config_service.getWechatConfig(site_id)[ 'app_id' ];//公众号appid
        //查询公众号配置
        const core_weapp_config_service = this.coreWeappConfigService;
        const mini_app_id = core_weapp_config_service.getWeappConfig(site_id)[ 'app_id' ];//小程序appid
        // 查询微信移动应用配置
        const core_app_service = this.coreAppService;
        const app_id = core_app_service.getConfig(site_id).wechat_app_id || ''; //App appid

        //todo  查询微信小程序 appid  .  应用appid.....
        return [
            mp_app_id: mp_app_id,
            mini_app_id: mini_app_id,
            app_id: app_id
            //............
        ;
  }
  }
}
